/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package riaxprez;

import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;

/**
 *
 * @author Vincent
 */
public class AppController {
    ViewAccueil viewAcc;
    AppModel model;
    ViewForm vf;
    ViewCreation viewCreate;
    ViewPrez viewPrez;
    
    public AppController(ViewAccueil viewAcc, AppModel model){
        this.viewAcc = viewAcc;
        this.model = model;
        setEventAcc();
    }
    
    public final void setEventAcc(){
        /* Evenement pour la vue viewAcc */
        /*--------------------------------------------------------------------*/
        viewAcc.btnNew.setOnAction(new EventHandler() {
            @Override
            public void handle(Event t) {
                vf = new ViewForm();
                setEventForm();
                viewAcc.stage.hide();
            }
        });
        
        viewAcc.btnOpen.setOnAction(new EventHandler<ActionEvent>(){

            @Override
            public void handle(ActionEvent t) {
                System.out.println("open project");
            }
            
        });
        
        /* 1 = francais, 2 = anglais, 3 = allemand */
        viewAcc.lbFr.setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent t) {
                viewAcc.setLangue(1);
                viewAcc.updateEffect(1);
            }
        });
        
        viewAcc.lbAng.setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent t) {
                viewAcc.setLangue(2);
                viewAcc.updateEffect(2);
            }
        });
        
        viewAcc.lbAll.setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent t) {
                viewAcc.setLangue(3);
                viewAcc.updateEffect(3);
            }
        });
    }
    
    
    @SuppressWarnings("unchecked")
	public void setEventForm(){
	    /* controller pour la vue viewForm */
	    /*--------------------------------------------------------------------*/
	    
	    vf.btnOK.setOnAction(new EventHandler() {
	        @Override
	        public void handle(Event t) {
                        /*XMLParser xmlparser = new XMLParser(new Element("config"));
                        String[] elt = new String[5];
                        elt[0] = tfNomPrenom.getText();
                        elt[1] = cb.getValue().toString();
                        elt[2] = tg.getSelectedToggle().getUserData().toString();
                        elt[3] = colorPicker.getValue().toString();
                        elt[4] = tfSave.getText();
                        xmlparser.setConfig(elt);*/
                        boolean valid = true;
                        
                        if(vf.cb.getValue()==null || vf.tfNomPrenom.getText().equals("") 
                                || vf.tfNomPrenom.getText().trim().equals("") || vf.tg.getSelectedToggle().getUserData()==null
                                || vf.tfSave.getText().equals("") || vf.tfSave.getText().trim().equals("")){
                            valid = false;
                        }
                            
                        if(valid){
                            /* Sauvegarde des elements du formulaire dans le modele */
                            model.setNomPrenom(vf.tfNomPrenom.getText());
                            model.setFiliere(vf.cb.getValue().toString());
                            model.setTransition(vf.tg.getSelectedToggle().getUserData().toString());
                            model.setColorTheme(vf.colorPicker.getValue().toString());
                            model.setPathSave(vf.tfSave.getText());
                            
                            vf.lbError.setText("");
                            viewCreate = new ViewCreation(model);
                            setEventCreate();
                            vf.primaryStage.hide();
                        }else{
                            vf.lbError.setText("Veuillez remplir tous les champs !");
                        }
	        }
	    });
    }
    
    /* controller pour la vue Creation */
    /*--------------------------------------------------------------------*/
    public void setEventCreate(){
        viewCreate.btnSwitchToPrez.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent t) {
                viewCreate.stage.hide();
                viewPrez = new ViewPrez(model);
                viewPrez.stage.show();
                setEventPrez();
            }
        });
    }
    
    /* controller pour la vue Prez */
    /*--------------------------------------------------------------------*/
    public void setEventPrez(){
        viewPrez.btnSwitchToCreation.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent t) {
                viewPrez.stage.hide();
                viewCreate.stage.show();
            }
        });
        
        viewPrez.btnFullScreen.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent t) {
                viewPrez.stageFullScreen.setScene(viewPrez.sceneFullScreen);
                viewPrez.stageFullScreen.setFullScreen(true);
                viewPrez.stageFullScreen.show();
            }
        });
        viewPrez.sceneFullScreen.setOnKeyPressed(new EventHandler<KeyEvent>(){
            public void handle(KeyEvent ke){
            	KeyCode ko = null;
            	if(ke.getCode()==ko.ESCAPE){
            		viewPrez.stageFullScreen.hide();
            	}
            	if(ke.getCode()==ko.RIGHT || ke.getCode()==ko.DOWN || ke.getCode()==ko.ENTER){
            		//changer de page 
            		viewPrez.changePage();
            	}
            	
                
            }
        });
        
        
    }
}